import { login } from "@api/user";
import { ref, reactive} from "vue";
import router from "@/router";
import { StorageSerializers, useStorage } from "@vueuse/core";
const userInfo = useStorage("userInfo", null, sessionStorage, {
    serializer: StorageSerializers.object
})
export const useUser = () => {
    const rememberPassword = ref(false);
    const loginModel = reactive({
        UserName: "",
        Password: "",
    });
    const rules = reactive({
        UserName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        Password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    });
    const logIn = () => {
        if (!loginModel.UserName) { return ElMessage.warning("请输入用户名"); }
        if (!loginModel.Password) { return ElMessage.warning("请输入密码"); }
        login(loginModel).then(res => {
            if (res.Status == "ok") {
                userInfo.value = {
                    UserName: loginModel.UserName,
                    Password: loginModel.Password,
                    token:res.Content.token
                }
                router.push("/Home")
            }
        })
    };
    const logOut=()=>{
        userInfo.value=null;
        router.push("/")
    }
    return {
        userInfo,
        rememberPassword,
        loginModel,
        rules,
        logIn,
        logOut
    }
}